package com.xb.loan.usercenter.user.user.service.impl;

import com.xb.loan.log.exception.BizException;
import com.xb.loan.usercenter.service.user.IUserMsgService;
import com.xb.loan.usercenter.service.user.bean.UserMsgVO;
import com.xb.loan.usercenter.service.user.exception.IUserServiceException;
import com.xb.loan.usercenter.user.user.dao.UserMsgDao;
import com.xb.loan.usercenter.user.user.dao.bean.UserMsg;
import com.xb.loan.util.cache.redis.comm.FormatOperation;
import com.xb.loan.util.common.ObjectOperation;
import com.xb.loan.util.strategy.TableNoAllocatorFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userMsgService")
public class UserMsgServiceImpl implements IUserMsgService,IUserServiceException {
	
	private static final String T_USER_MSG = "t_user_msg";
	
	@Autowired
	UserMsgDao userMsgDao;
	
	@Override
	public void add(UserMsgVO userMsgVO) throws BizException {
		
		if(userMsgVO.getUserId() == null || "".equals(userMsgVO.getUserId())){
			throw userIdMissing;
		}
		UserMsg userMsg = ObjectOperation.convertByClasses(userMsgVO, UserMsg.class);
        String id =  FormatOperation.assemblySeq("common", 4, FormatOperation.formatDateToYMDHms(new Date()), "");
        userMsg.setId(id);
		userMsg.setTableId(TableNoAllocatorFactory.getAllocator(T_USER_MSG).getTableNo(userMsg.getUserId()));
		userMsgDao.insertSelective(userMsg);
	}

	@Override
	public void deleteById(String id, String userId) throws BizException {
		
		if(userId == null || "".equals(userId)){
			throw userIdMissing;
		}
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id", id);
		map.put("tableId", TableNoAllocatorFactory.getAllocator(T_USER_MSG).getTableNo(userId));
		userMsgDao.deleteById(map);
	}

	@Override
	public void deleteBatch(String sysId, String tableId) throws BizException {
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("sysId", sysId);
		map.put("tableId", tableId);
		userMsgDao.deleteBatch(map);
	}

	@Override
	public void updateById(UserMsgVO userMsgVO) throws BizException {
		
		if(userMsgVO.getUserId() == null || "".equals(userMsgVO.getUserId())){
			throw userIdMissing;
		}
		UserMsg userMsg = ObjectOperation.convertByClasses(userMsgVO, UserMsg.class);
		userMsg.setTableId(TableNoAllocatorFactory.getAllocator(T_USER_MSG).getTableNo(userMsg.getUserId()));
		userMsgDao.updateById(userMsg);
	}

	@Override
	public void updateBatch(String sysId, String title, String content, String imgUrl, String tableId)
			throws BizException {
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("sysId", sysId);
		map.put("title", title);
		map.put("content", content);
		map.put("imgUrl", imgUrl);
		map.put("tableId", tableId);
		userMsgDao.updateBatch(map);
	}
	
	@Override
	public UserMsgVO queryById(String id, String userId) throws BizException {
		
		if(userId == null || "".equals(userId)){
			throw userIdMissing;
		}
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id", id);
		map.put("tableId", TableNoAllocatorFactory.getAllocator(T_USER_MSG).getTableNo(userId));
		UserMsg userMsg =  userMsgDao.queryById(map);
		return ObjectOperation.convertByClasses(userMsg, UserMsgVO.class);
	}

	@Override
	public List<UserMsgVO> queryUserMsg(String userId, String title, String content, Integer type, Integer status,
                                        String startTime, String endTime, int pageNum, int numPerPage) throws BizException {
		
		if(userId == null || "".equals(userId)){
			throw userIdMissing;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tableId", TableNoAllocatorFactory.getAllocator(T_USER_MSG).getTableNo(userId));
		map.put("userId", userId);
		map.put("title", title);
		map.put("content", content);
		map.put("type", type);
		map.put("status", status);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		map.put("pageNum", pageNum);
		map.put("numPerPage", numPerPage);
		List<UserMsg> userMsgList = userMsgDao.queryUserMsg(map);
		return ObjectOperation.convertByClassesList(userMsgList, UserMsgVO.class);
	}
	
	@Override
	public int queryUserMsgCount(String userId, String title, String content, Integer type, Integer status,
                                 String startTime, String endTime) throws BizException {
		
		if(userId == null || "".equals(userId)){
			throw userIdMissing;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tableId", TableNoAllocatorFactory.getAllocator(T_USER_MSG).getTableNo(userId));
		map.put("userId", userId);
		map.put("title", title);
		map.put("content", content);
		map.put("type", type);
		map.put("status", status);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		return userMsgDao.queryUserMsgCount(map);
	}

	@Override
	public List<UserMsgVO> queryUserMsg(List<String> sysidList, String userId) throws BizException {
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sysidList", sysidList);
		map.put("tableId", TableNoAllocatorFactory.getAllocator(T_USER_MSG).getTableNo(userId));
		List<UserMsg> userMsgList = userMsgDao.queryUserMsgBySysid(map);
		return ObjectOperation.convertByClassesList(userMsgList, UserMsgVO.class);
	}

}
